/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bsc.util.json;

/**
 *
 * @author peike.ma
 */
//import com.glu.platform.obj.Models;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

/**
 * Title: Ext JS 辅助类
 * Description: 该类用于转换java对象为XML文件格式或JSON文件格式
 * @author weijun
 * @time: 2008.07.09
 */
public class ExtHelper {

    /**
     * 通过List生成XML数据
     * @param recordTotal 记录总数，不一定与beanList中的记录数相等
     * @param beanList 包含bean对象的集合
     * @return 生成的XML数据
     */
    public static String getXmlFromList(int recordTotal, List beanList) {
        Total total = new Total();
        total.setTotal(recordTotal);
        List results = new ArrayList();
        results.add(total);
        results.addAll(beanList);
        XStream sm = new XStream(new DomDriver());
        for (int i = 0; i < results.size(); i++) {
            Class c = results.get(i).getClass();
            String b = c.getName();
            String[] temp = b.split("\\.");
            sm.alias(temp[temp.length - 1], c);
        }
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + sm.toXML(results);
        return xml;
    }

    /**
     * 通过List生成XML数据
     * @param beanList 包含bean对象的集合
     * @return 生成的XML数据
     */
    public static String getXmlFromList(List beanList, int Bsize) {
        return getXmlFromList(Bsize, beanList);
    }

    /**
     * 通过List生成JSON数据
     * @param recordTotal 记录总数，不一定与beanList中的记录数相等
     * @param beanList 包含bean对象的集合
     * @return 生成的JSON数据
     */
    public static String getJsonFromList(long recordTotal, List beanList, String start, String limit, String search) {
        TotalJson total = new TotalJson();
        total.setTotal(recordTotal);
        total.setItems(beanList);
        total.setStart(start);
        total.setLimit(limit);
        total.setSearch(search);
        JSONObject JsonObject = JSONObject.fromObject(total);
        return JsonObject.toString();
    }

    /**
     * 通过List生成JSON数据
     * @param beanList 包含bean对象的集合
     * @return 生成的JSON数据
     */
    public static String getJsonFromList(List beanList, String start, String limit, String search) {
        return getJsonFromList(beanList.size(), beanList, start, limit, search);
    }

    public static List getListFormJson(String json, Class myclass) {
        JSONArray JsonArray = JSONArray.fromObject(json);
        return (List) JSONArray.toCollection(JsonArray, myclass);
    }

    /**
     * 通过bean生成JSON数据
     * @param bean bean对象
     * @return 生成的JSON数据
     */
    public static String getJsonFromBean(Object bean) {
        JSONObject JsonObject = JSONObject.fromObject(bean);
        return JsonObject.toString();
    }

    public static Object getBeanFormJson(String json, Class mycalss) {
        JSONObject JsonObject = JSONObject.fromObject(json);
        return JSONObject.toBean(JsonObject, mycalss);
    }

    //json 过滤循环字段
    public static JsonConfig groupProperty(final String[] str) {
        JsonConfig config = new JsonConfig();
        config.setJsonPropertyFilter(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                if (str != null && str.length > 0) {
                    boolean flag = false;
                    for (String strs : str) {
                        if (name.equals(strs)) {
                            flag = true;
                            break;
                        }
                    }
                    return flag;
                } else {
                    return false;
                }
            }
        });
        return config;
    }

//    public static void main(String[] arg) {
//        String json = "[{'name':'Card','text':'','pre':'添加非跳转card标签头'},{'name':'Text','text':'文本控件','pre':'显示文本文字控件'}]";
//        List<Models> list = getListFormJson(json, Models.class);
//        if (list != null) {
//            for (Models object : list) {
//                System.out.println(object.toString());
//            }
//        }
//    }
}
